package UILayer;
import java.util.Scanner;
import ControlLayer.*;

/**
 * Write a description of class CustomerUI here.
 * 
 * @author (Gruppe 2) 
 * @version (a version number or a date)
 */
public class CustomerUI
{
    private CustomerCtr cusCtr; //reference to Customer in the ControlLayer
    public CustomerUI()
    {
        cusCtr = new CustomerCtr();
        start(cusCtr);
    }

    public void start(CustomerCtr cusCtr)
    {
        this.cusCtr = cusCtr;
        customerMenu();
    }

    public void customerMenu()
    {
        boolean exit = false;
        while(!exit)
        {
            int choice = writeCustomerMenu();
            if(choice == 1)
            {   //show Customer
                System.out.println("\f");
                printCustomer(inputID());
            }
            if(choice == 2)
            {   //create Customer
                System.out.println("\f");
                cusCtr.createCustomer(inputID(), inputName(), inputAddress(), inputPhone(), inputMail(), inputCPR());
            }
            if(choice == 3)
            {
                System.out.println("\f");
                deleteCustomer(inputID());
            }
            if(choice == 4)
            {
                System.out.println("\f");
                printAllCustomers();
            }
            if(choice == 5)
            {
                System.out.println("\f");
                printBusiness(inputID());
            }
            if(choice == 6)
            {
                System.out.println("\f");
                cusCtr.createBusiness(inputID(), inputName(), inputAddress(), inputPhone(), inputMail(), inputCompany(), inputCVR(), inputDiscount());
            }
            if(choice == 7)
            {
                System.out.println("\f");
                deleteBusiness(inputID());
            }
            if(choice == 8)
            {
                System.out.println("\f");
                printAllBusiness();
            }
            if(choice == 9)
            {
                exit = true;
                System.out.println("\f");
            }
        }
    }

    public int writeCustomerMenu()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println(""); //This blank line are made so that everything wont be printed right on top of each other
        System.out.println("*** Customer Menu ***");
        System.out.println(" (1) Show information about customer");
        System.out.println(" (2) Add customer");
        System.out.println(" (3) Delete customer");
        System.out.println(" (4) Show all customers");
        System.out.println(" (5) Show information about business customer");
        System.out.println(" (6) Add business customer");
        System.out.println(" (7) Delete business customer");
        System.out.println(" (8) show all business customers");
        System.out.println(" (9) Return");
        System.out.print("\n Make your choice: ");

        boolean done = false;
        int choice = 0;
        while(!done)
        {
            try
            {
                choice = keyboard.nextInt();
                done = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number - try again");
                String input = keyboard.nextLine();
            }
        }
        return choice;
    }

    public int inputID()
    {
        boolean done = false;
        int id = 0;
        while(!done)
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println( "Write customer's ID:  ");
            try
            {
                id = keyboard.nextInt();
                done = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number - try again");
                String input = keyboard.nextLine();
            }
        }
        return id;
    }

    public String inputName()
    {
        // makes an object keyboard to have input from the screen
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Write Name:  ");
        String name = keyboard.nextLine();
        return name;
    }

    public String inputPostalCode()
    {
        // makes an object keyboard to have input from the screen
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Write Postal Code:  ");
        String code = keyboard.nextLine();
        return code;
    }

    public String inputCity()
    {
        // makes an object keyboard to have input from the screen
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Write City:  ");
        String city = keyboard.nextLine();
        return city;
    } 

    public String inputPhone()
    {
        // makes an object keyboard to have input from the screen
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Write Phone Number:  ");
        String phone = keyboard.nextLine();
        return phone;
    } 

    public String  inputAddress()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write Address: ");
        String address = keyboard.nextLine();
        return address;
    }

    public String inputMail()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write e-mail: ");
        String mail = keyboard.nextLine();
        return mail;
    }

    public String inputCPR()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write CPR: ");
        String CPRnr = keyboard.nextLine();
        return CPRnr;
    }
    
    public String inputCompany()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write Company name: ");
        String companyName = keyboard.nextLine();
        return companyName;
    }
    
    public String inputCVR()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write CVR: ");
        String CVRnr = keyboard.nextLine();
        return CVRnr;
    }
    
    public double inputDiscount()
    {
        boolean done = false;
        double discount = 0;
        while(!done)
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println(" Write discount: ");
            try
            {
                discount = keyboard.nextDouble();
                done = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number - try again");
                String input = keyboard.nextLine();
            }
        }
        return discount;
    }

    public void showId(int id)
    {
        System.out.println("ID of the customer: " + id);
    }

    public void showName(String name)
    { 
        System.out.println("The name of the customer: " + name);
    }

    public void showAddress(String address)
    { 
        System.out.println("The address of the customer: " + address);
    }

    private void printCustomer(int id)
    {
        cusCtr.printCustomer(id);
    }   

    private void deleteCustomer(int id)
    {
        cusCtr.removeCustomer(id);
    }
    
    private void printAllCustomers()
    {
        cusCtr.printCustomerList();
    }
    
    private void printBusiness(int id)
    {
        cusCtr.printBusiness(id);
    }   

    private void deleteBusiness(int id)
    {
        cusCtr.removeBusiness(id);
    }
    
    private void printAllBusiness()
    {
        cusCtr.printBusinessList();
    }

}
